package utilities.utilities_components;

import org.zkoss.zul.Chart;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.SimpleCategoryModel;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Window;

import org.zkoss.zk.ui.Executions;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import utilities.Updatable;

public class NetDailyGridTab extends Window implements Updatable{
	private Tabbox panel;
	private Tab tab;
	private Tabs tabs;
	private Tabpanel tabPanel;
	private Tabpanels tabPanels;
	private GridBox grid;
	private Chart cate;
	private SimpleCategoryModel model;
	
	private int col;
	
	private static final Calendar calendar =  Calendar.getInstance();
	
	public NetDailyGridTab( String[] columns){
		/*  Create components  */
		grid = new GridBox( columns );
		
		col = columns.length;
		
		initialChart();
		
		/*  Create tabbox  */
		panel = new Tabbox();
		
		createTabbox();
		
		this.appendChild( panel );
	}
	
	@Override
	public void setDisable(boolean disabled) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void update( List data ){
		if( data == null ){
			try{
				new Messagebox().show( "更新失败!" );				
			}catch( InterruptedException exception ){
				/*  Ignore  */
			}
			
			return;
		}
		
		model.clear();
		
		List rows = grid.getChildren();
		ArrayList cast = ( ArrayList )data;
		ArrayList colInfo;
		
		for( int row = 2; row < 18; row++ ){
			int dataCur = 0;
			int itemCur = 1;
			
			colInfo = ( ArrayList )cast.get( row );
			Row currRow = ( Row )rows.get( row );
			List rowItem = currRow.getChildren();
			( ( Label )rowItem.get( itemCur++ ) ).setValue( colInfo.get( dataCur++ ).toString() );
			
			for( ; dataCur < colInfo.size(); dataCur++ ){
				( ( Doublebox )rowItem.get( itemCur++ ) ).setValue( new Double( colInfo.get( dataCur ).toString() ).doubleValue() );
			}
			
			dataCur++;
			
			colInfo= ( ArrayList )cast.get( row + 16 );
			( ( Label )rowItem.get( itemCur++ ) ).setValue( colInfo.get( dataCur++ ).toString() );
			
			for( ; dataCur < colInfo.size(); dataCur++ ){
				( ( Doublebox )rowItem.get( itemCur++ ) ).setValue( new Double( colInfo.get( dataCur ).toString() ).doubleValue() );
			}
		}
	}
	
	private void createTabbox(){
		/*  Create tabs  */
		tabs = new Tabs();
		panel.appendChild( tabs );
		
		/*  Create tab  */
		tab = new Tab( "报表" );
		tabs.appendChild( tab );
		
		tab = new Tab( "图表" );
		tabs.appendChild( tab );
		
		/*  Create tab panels  */
		tabPanels = new Tabpanels();
		panel.appendChild( tabPanels );
		
		/*  Create panels  */
		tabPanel = new Tabpanel();
		tabPanel.appendChild( ( Window )grid );
		tabPanels.appendChild( tabPanel );
		
		tabPanel = new Tabpanel();
		tabPanel.appendChild( cate );
		tabPanels.appendChild( tabPanel );
	}
	
	private void initialChart(){
		cate = new Chart();
		model = new SimpleCategoryModel();
		
		cate.setModel( model );
		cate.setThreeD( true );
		cate.setType( "line" );
		cate.setFgAlpha( 128 );
	}
}
